package com.example.first;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class CurrencyActivity extends AppCompatActivity implements Runnable{
    private static final String TAG = "CurrencyActivity";

    EditText rmb;
    TextView result;

    private float dollarRate =  0.0f;
    private  float euroRate =  0.0f;
    private float wonRate =  0.0f;

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_currency);

        rmb = findViewById(R.id.input);
        result = findViewById(R.id.result);

        SharedPreferences sharedPreferences = getSharedPreferences("myrate", Activity.MODE_PRIVATE);
        dollarRate = sharedPreferences.getFloat("dollar_rate",0.0f);
        euroRate = sharedPreferences.getFloat("euro_rate",0.0f);
        wonRate = sharedPreferences.getFloat("won_rate",0.0f);
        Log.i(TAG,"onCreate:dollarRate =" + dollarRate);
        Log.i(TAG,"onCreate:euroRate =" + euroRate);
        Log.i(TAG,"onCreate:wonRate =" + wonRate);


        //开启线程
        Thread t = new Thread(this);
        t.start();


    }


    public void Onclick (View btn) {
        Log.i(TAG,"onClick:");
        String str = rmb.getText().toString();
        Log.i(TAG,"onClick:get str" + str);

        float r = 0.0f;
        if (str != null && str.length() > 0) {
            r = Float.parseFloat(str);
        } else {
            //提示用户输入
            Toast.makeText(this, "请输入RMB金额", Toast.LENGTH_SHORT).show();
        }

        Log.i(TAG,"Onclick:r=" + r);

        //计算
        if (btn.getId() == R.id.btnDOLLAR) {
           result.setText(String.format("%.2f",r*dollarRate));
        } else if (btn.getId() == R.id.btnEURO) {
            result.setText(String.format("%.2f",r*euroRate));
        } else {
            result.setText(String.format("%.2f",r*wonRate));
        }
    }

        public void openConfig(View btn){
            Intent config = new Intent(this,ConfigActivity.class);
            config.putExtra("dollar_key",dollarRate);
            config.putExtra("euro_key",euroRate);
            config.putExtra("won_key",wonRate);

            Log.i(TAG,"openOne:dollarRate=" + dollarRate);
            Log.i(TAG,"openOne:euroRate=" + euroRate);
            Log.i(TAG,"openOne:wonRate=" + wonRate);

            startActivityForResult(config,3);

        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if(requestCode==3 && resultCode == 6){
                Bundle bundle = data.getExtras();
                dollarRate = bundle.getFloat("key_dollar", 0.1f);
                euroRate = bundle.getFloat("key_euro", 0.1f);
                dollarRate = bundle.getFloat("key_won", 0.1f);
                Log.i(TAG,"onActivityResult:dollarRate=" + dollarRate);
                Log.i(TAG,"onActivityResult:euroRate=" + euroRate);
                Log.i(TAG,"onActivityResult:wonRate=" + wonRate);

                 SharedPreferences sp = getSharedPreferences("myrate", Activity.MODE_PRIVATE);
                 SharedPreferences.Editor editor = sp.edit();
                 editor.putFloat("dollar_rate",dollarRate);
                 editor.putFloat("euro_rate",euroRate);
                 editor.putFloat("won_rate",wonRate);
                 editor.commit();
                 Log.i(TAG,"onActivityResult:数据已保存到SharedPreferences" );

            }

            super.onActivityResult(requestCode,resultCode,data);
        }

    @Override
    public void run() {
        Log.i(TAG,"run:run().......");
        for(int i=1;i<3;i++){
            Log.i(TAG,"run:i="+i);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //获取Msg对象，用于返回主线程
        Message msg = handler.obtainMessage(5);
        msg.obj = "Hello from run()";
        handler.sendMessage(msg);
    }
}
